package core.cards.election.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import core.cards.FormFrontend;
import core.cards.election.model.ElectionBackend;
import core.cards.election.model.VotingCard;
import core.cards.election.model.VotingCardOption;

public class ElectionFrontend extends FormFrontend {
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public ElectionFrontend(Composite parent, ElectionBackend electionBackend) {
		super(parent, SWT.NONE, electionBackend);
		setLayout(new GridLayout(1, false));

		TabFolder electionTbFldr = new TabFolder(this, SWT.NONE);
		electionTbFldr.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		for (VotingCard votingCard : electionBackend.getVotingCardList()) {
			new VotingCardTbTm(electionTbFldr, votingCard);
		}
	}
}

class VotingCardTbTm extends TabItem {
	private VotingCard votingCard;

	private Text txtDescription;
	private Label lblError;

	public VotingCardTbTm(TabFolder parent, VotingCard votingCard) {
		super(parent, SWT.NONE);

		this.votingCard = votingCard;

		setText(votingCard.getName());

		Composite votingCardCmp = new Composite(parent, SWT.NONE);
		setControl(votingCardCmp);
		votingCardCmp.setLayout(new GridLayout(1, false));

		txtDescription = new Text(votingCardCmp, SWT.BORDER | SWT.WRAP | SWT.MULTI);
		GridData gdTxtDescription = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gdTxtDescription.heightHint = 100;
		txtDescription.setLayoutData(gdTxtDescription);
		txtDescription.setEditable(false);
		txtDescription.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		txtDescription.setText(votingCard.getDescription());

		lblError = new Label(votingCardCmp, SWT.NONE);
		lblError.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lblError.setText("Too many options checked. This cards vote will not be counted.");
		lblError.setVisible(false);

		Group group = new Group(votingCardCmp, SWT.NONE);
		group.setLayout(new GridLayout(1, false));
		group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));

		for (VotingCardOption votingCardOption : votingCard.getVotingCardOptionList()) {
			new VotingCardCheckbox(group, this, votingCardOption);
		}
	}

	public void validate() {
		lblError.setVisible(!votingCard.isValid());
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}

class VotingCardCheckbox extends Button {
	private VotingCardOption votingCardOption;
	private VotingCardTbTm votingCardTbTm;

	public VotingCardCheckbox(Composite parent, VotingCardTbTm votingCardTbTm, VotingCardOption votingCardOption) {
		super(parent, SWT.CHECK);

		this.votingCardTbTm = votingCardTbTm;
		this.votingCardOption = votingCardOption;

		setText(votingCardOption.getName());

		addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				VotingCardCheckbox.this.votingCardOption.check();
				VotingCardCheckbox.this.votingCardTbTm.validate();
			}
		});
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}